--[[
	GMod Last Man Standing, a gamemode for Garry's Mod
    Copyright (C) 2012 Wickex

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

--[[resource.Addfile("materials/apple/apple.vmt")
resource.Addfile("materials/apple/apple.vtf")
resource.Addfile("models/apple/apple.dx80.vtx")
resource.Addfile("models/apple/apple.dx90.vtx")
resource.Addfile("models/apple/apple.mdl")
resource.Addfile("models/apple/apple.phy")
resource.Addfile("models/apple/apple.sw.vtx")
resource.Addfile("models/apple/apple.vvd")
resource.Addfile("models/apple/apple.xbox.vtx")]]

include("shared.lua")

local random = CurTime() + math.random(1, 65535) * math.random(1, 100)

function ENT:KeyValue(key, value)
	if key == "food_respawntime" then
		self.respawntime = tonumber(value) or 3
	elseif key == "food_respawnamount" then
		self.respawnamount = tonumber(value) or -1
	end
end

function ENT:AcceptInput( name, activator, caller, data )
	if name == "food_respawn" then
		self:Respawn()
	end
end

function ENT:Initialize()
	self:SetModel("models/apple/apple.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	local phys = self:GetPhysicsObject()

	phys:Wake()

	self.damage = 15
end

function ENT:OnTakeDamage(dmg)
	self.damage = self.damage - dmg:GetDamage()

	if (self.damage <= 0) then
		local effectdata = EffectData()
		effectdata:SetOrigin(self:GetPos())
		effectdata:SetMagnitude(2)
		effectdata:SetScale(2)
		effectdata:SetRadius(3)
		util.Effect("Sparks", effectdata)
		self:Remove()
	end
end

function ENT:Use(activator, caller)
	caller:SetHealth(math.min(caller:Health() + 25, 100))
	if GetConVar("glms_hunger"):GetInt() == 1 then
		--caller:SetSelfVar("Hunger", math.min(caller.servervars.Hunger + 25, 100))
	end
	--umsg.Start("AteFood", caller)
	--umsg.End()
	self:Remove()
	activator:EmitSound("vo/sandwicheat09.wav", 100, 100)
end

food_timers = { }

function ENT:OnRemove()
	self:TriggerOutput("food_onpickup", self)
	if self.respawnamount > 0 or self.respawnamount == -1 then
		local timertime = random
		timer.Create(timertime, self.respawntime * 60, 1, self:Respawn())
		table.insert(food_timers, timertime)
	end
end